package com.ruoyi.system.controller;

import java.util.Collections;
import java.util.List;

import com.ruoyi.system.enservice.IAlarmEnService;
import com.ruoyi.system.enservice.impl.AlarmEnServiceImpl;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.GaAlarm;
import com.ruoyi.system.service.IGaAlarmService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 报警Controller
 *
 * @author wzp
 * @date 2021-11-18
 */
@RestController
@RequestMapping("/kyga/alarm")
public class GaAlarmController extends BaseController {
    @Autowired
    private IGaAlarmService gaAlarmService;

    @Autowired
    private IAlarmEnService alarmEnService;

    /**
     * 查询报警列表
     */
    @PreAuthorize("@ss.hasPermi('kyga:alarm:list')")
    @GetMapping("/list")
    public TableDataInfo list(GaAlarm gaAlarm) {
        startPage();
        List<GaAlarm> list = gaAlarmService.selectGaAlarmList(gaAlarm);
        return getDataTable(list);
    }

    /**
     * 导出报警列表
     */
    @PreAuthorize("@ss.hasPermi('kyga:alarm:export')")
    @Log(title = "报警", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(GaAlarm gaAlarm) {
        List<GaAlarm> list = gaAlarmService.selectGaAlarmList(gaAlarm);
        ExcelUtil<GaAlarm> util = new ExcelUtil<GaAlarm>(GaAlarm.class);
        return util.exportExcel(list, "报警数据");
    }

    /**
     * 获取报警详细信息
     */
    @PreAuthorize("@ss.hasPermi('kyga:alarm:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(gaAlarmService.selectGaAlarmById(id));
    }

    /**
     * 新增报警
     */
    @PreAuthorize("@ss.hasPermi('kyga:alarm:add')")
    @Log(title = "报警", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GaAlarm gaAlarm) {
        return toAjax(gaAlarmService.insertGaAlarm(gaAlarm));
    }

    /**
     * 修改报警
     */
    @PreAuthorize("@ss.hasPermi('kyga:alarm:edit')")
    @Log(title = "报警", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GaAlarm gaAlarm) {
        if(alarmEnService.editAlarm(gaAlarm,getUserId())) {
            return AjaxResult.success();
        }
        return AjaxResult.error();

    }

    /**
     * 删除报警
     */
    @PreAuthorize("@ss.hasPermi('kyga:alarm:remove')")
    @Log(title = "报警", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(gaAlarmService.deleteGaAlarmByIds(ids));
    }

    /**
    * 查询报警历史
    */
    @GetMapping("/searchStudentAlarmHistoryList/{studentId}")
    public TableDataInfo searchStudentAlarmHistoryList(@PathVariable("studentId") Long creatUser) {
        //startPage();
        List<GaAlarm> list = gaAlarmService.selectGaAlarmStudentList(creatUser);
        return getDataTable(list);
    }

    /**
     * 根据学生ID和报警ID查询报警的详细信息
     */
    @GetMapping("/searchAlarmDetail")
    public TableDataInfo searchAlarmDetaile(@RequestParam Long studentId,@RequestParam Long alarmId) {
        GaAlarm gaAlarm = gaAlarmService.selectGaAlarmById(alarmId);
        return getDataTable(Collections.singletonList(gaAlarm));
    }
}
